import { createRouter, createWebHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import { useUserStore } from '@/stores/user'
import Layout from '@/layout/index.vue'

// 路由配置
const routes: RouteRecordRaw[] = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/login/index.vue'),
    meta: { title: '管理员登录', noLayout: true }
  },
  {
    path: '/phone-login',
    name: 'PhoneLogin',
    component: () => import('@/views/phone-login/index.vue'),
    meta: { title: '普通用户登录', noLayout: true }
  },
  {
    path: '/',
    name: 'Home',
    component: () => import('@/views/home/index.vue'),
    meta: { title: '健康资讯', requiresAuth: false, noLayout: true }
  },
  {
    path: '/posts/:id',
    name: 'PostDetail',
    component: () => import('@/views/health/post-detail.vue'),
    meta: { title: '帖子详情', requiresAuth: true, noLayout: true }
  },
  {
    path: '/app',
    component: Layout,
    redirect: '/app/health/dashboard',
    children: [
      // 用户管理（仅管理员）
      {
        path: 'users',
        name: 'Users',
        component: () => import('@/views/users/index.vue'),
        meta: { title: '用户管理', icon: 'User', requiresAuth: true, adminOnly: true }
      },
      // 健康数据记录（普通用户）
      {
        path: 'health',
        name: 'Health',
        redirect: '/app/health/dashboard',
        meta: { title: '健康数据', icon: 'User', requiresAuth: true },
        children: [
          {
            path: 'dashboard',
            name: 'HealthDashboard',
            component: () => import('@/views/health/dashboard.vue'),
            meta: { title: '数据面板', icon: 'DataAnalysis' }
          },
          {
            path: 'body-records',
            name: 'BodyRecords',
            component: () => import('@/views/health/body-records.vue'),
            meta: { title: '身体指标记录', icon: 'User' }
          },
          {
            path: 'eating-records',
            name: 'EatingRecords',
            component: () => import('@/views/health/eating-records.vue'),
            meta: { title: '饮食记录', icon: 'User' }
          },
          {
            path: 'sleep-records',
            name: 'SleepRecords',
            component: () => import('@/views/health/sleep-records.vue'),
            meta: { title: '睡眠记录', icon: 'User' }
          },
          {
            path: 'sports-records',
            name: 'SportsRecords',
            component: () => import('@/views/health/sports-records.vue'),
            meta: { title: '运动记录', icon: 'User' }
          },
          {
            path: 'posts',
            name: 'UserPosts',
            component: () => import('@/views/health/posts.vue'),
            meta: { title: '我的帖子', icon: 'Document' }
          },
          {
            path: 'forum-center',
            name: 'ForumCenter',
            component: () => import('@/views/health/forum-center.vue'),
            meta: { title: '论坛中心', icon: 'ChatDotRound' }
          },
          {
            path: 'my-favorites',
            name: 'MyFavorites',
            component: () => import('@/views/health/my-favorites.vue'),
            meta: { title: '我的收藏', icon: 'Star' }
          },
          {
            path: 'health-reports',
            name: 'HealthReports',
            component: () => import('@/views/health/health-reports.vue'),
            meta: { title: '体检报告', icon: 'Document' }
          }
        ]
      },
      // 系统管理（仅管理员）
      {
        path: 'system',
        name: 'System',
        redirect: '/app/system/notices',
        meta: { title: '系统管理', icon: 'Setting', requiresAuth: true, adminOnly: true },
        children: [
          {
            path: 'notices',
            name: 'Notices',
            component: () => import('@/views/system/notices.vue'),
            meta: { title: '公告管理', icon: 'ChatDotRound' }
          },
          {
            path: 'sensitive-words',
            name: 'SensitiveWords',
            component: () => import('@/views/system/sensitive-words.vue'),
            meta: { title: '敏感词管理', icon: 'Lock' }
          },
          {
            path: 'posts',
            name: 'Posts',
            component: () => import('@/views/system/posts.vue'),
            meta: { title: '帖子管理', icon: 'Document' }
          },
          {
            path: 'comments',
            name: 'Comments',
            component: () => import('@/views/system/comments.vue'),
            meta: { title: '评论管理', icon: 'ChatLineRound' }
          },
          {
            path: 'scheduled-tasks',
            name: 'ScheduledTasks',
            component: () => import('@/views/system/scheduled-tasks.vue'),
            meta: { title: '定时任务', icon: 'Clock' }
          }
        ]
      },
      // 个人中心
      {
        path: 'profile',
        name: 'Profile',
        component: () => import('@/views/profile/index.vue'),
        meta: { title: '个人中心', icon: 'User', requiresAuth: true }
      }
    ]
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('@/views/error/404.vue')
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, _from, next) => {
  const userStore = useUserStore()
  
  // 设置页面标题
  if (to.meta.title) {
    document.title = `${to.meta.title} - 健康管理`
  }
  
  // 检查是否需要认证
  if (to.meta.requiresAuth && !userStore.token) {
    next('/login')
    return
  }
  
  // 检查管理员权限
  if (to.meta.adminOnly && !userStore.isAdmin) {
    next('/')
    return
  }
  
  next()
})

export default router